package com.gai.web.interview.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gai.web.system.entity.Message;
import com.gai.web.interview.entity.GInterview;
import com.gai.web.interview.service.GInterviewService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author Captain
 * @since 2022-5-11
 */
@RestController
@Api(value="采访",tags="采访")
@RequestMapping("/interview")
public class GInterviewController {

    @Autowired
    private  GInterviewService gInterviewService;

    @ApiOperation(value="分页查询",notes="分页查询")
    @GetMapping("/list")
    public Message queryPageList(@RequestParam(name = "pageNo", defaultValue = "1")Integer pageNo,
                                 @RequestParam(name = "pageSize", defaultValue = "3")Integer pageSize,
                                 @RequestParam(name = "searchName",required=false)String searchName,
                                 @RequestParam(name = "startDate",required=false)String startDate,
                                 @RequestParam(name = "endDate",required=false)String endDate){
        QueryWrapper<GInterview> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(searchName), "title", searchName);
        queryWrapper.ge(StringUtils.hasText(startDate), "date", startDate);
        queryWrapper.le(StringUtils.hasText(endDate), "date", endDate);
        queryWrapper.orderByDesc("date");
        Page<GInterview> page= new Page<GInterview>(pageNo, pageSize);
        IPage<GInterview> pageList=gInterviewService.page(page,queryWrapper);
        return Message.success("获取成功！").add("pageList", pageList);
    }
    @ApiOperation(value="总数统计",notes="总数统计")
    @GetMapping("/getCount")
    public Message queryCount(){
        QueryWrapper<GInterview> queryWrapper = new QueryWrapper<>();
        int interview_num=gInterviewService.count();
        return Message.success("获取成功! ").add("interview_num", interview_num);
    }
    /**
     * 新增专访
     */
    @PostMapping("/add")
    @ApiOperation(value="新增采访",notes="新增采访")
    public Message add(@RequestBody GInterview gInterview){
        if (gInterviewService.save(gInterview)) {
            return Message.success("新增成功!");
        } else {
            return Message.error("新增失败!");
        }
    }
    /**
     * 编辑专访信息
     */
    @PostMapping("/edit")
    @ApiOperation(value="编辑采访",notes="编辑采访")
    public Message editAlbum(@RequestBody GInterview gInterview){
        if(gInterviewService.updateById(gInterview)){
            return Message.success("更新成功！");
        }else{
            return Message.error("更新失败！");
        }
    }
    /**
     * 删除专访信息
     */
    @DeleteMapping("/delete")
    @ApiOperation(value="删除采访",notes="删除采访")
    public Message deleteAlbum(@RequestParam(name = "id")Integer id){
        if (gInterviewService.removeById(id)) {
            return Message.success("删除成功");
        }else {
            return Message.error("该专辑下存在歌曲，无法删除");
        }
    }

}
